package com.cyh;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

/**
 * Created by yanhuche on 9/29/2016.
 */
public class ByValue__ByReference {


    @Test
    public void test01() {
        List<Counter> counterList = new ArrayList<>();

        Counter counter = new Counter("A", "one");
        counterList.add(counter);

        counter = new Counter("B", "two");
        counterList.add(counter);

        System.out.println(counterList);
    }

    @Test
    public void test02() {
        List<Counter> counterList = new ArrayList<>();

        Counter counter = new Counter("A", "one");
        counterList.add(counter);

        counter.setName("B");
        counter.setValue("two");
        counterList.add(counter);

        System.out.println(counterList);
    }

    @Test
    public void test03() {
        List<Counter> counterList = new ArrayList<>();

        Counter counter = new Counter("A", "one");
        counterList.add(counter);

        Measurement measurement = new Measurement();
        measurement.setCounterList(counterList);

        counterList.clear();
        counterList.add(new Counter("B", "two"));
        Measurement measurement2 = new Measurement();
        measurement2.setCounterList(counterList);

        System.out.println(measurement.getCounterList());
        System.out.println(measurement2.getCounterList());
    }

    class Measurement {
        List<Counter> counterList;

        public List<Counter> getCounterList() {
            return counterList;
        }

        public void setCounterList(List<Counter> counterList) {
            this.counterList = counterList;
        }
    }

    class Counter {
        String name;
        String value;

        public Counter(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setValue(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Counter {" + "name='" + name + '\'' + ", value='" + value + '\'' + '}';
        }
    }


}
